package vinylInt
{
    import flash.events.*;
    import flash.net.*;
	import flash.utils.*;
	import flash.system.*;
	import flash.system.System;
	import flash.display.*;
	import flash.display.Stage;
	
	/**
	 * 
	 */
	public class VinylIntISConfiguration extends EventDispatcher
	{
		private static var instance:VinylIntISConfiguration;
		public static var path:String;
		
		public var url:String;
		public var exeFlag:Boolean;
		public var debugFlag:Boolean;
		public var localFlag:Boolean;
		
		public var title:String;
		public var scope:MovieClip;
		public var outputFlag:Boolean;
		public var initFlag:Boolean;
		
		public function VinylIntISConfiguration() 
		{
			if(instance != null)
			{
				throw new Error("VinylIntISConfiguration Singleton already constructed!");
			}
		}
		
		public static function getInstance():VinylIntISConfiguration
		{
			if(instance == null)
			{
				instance=new VinylIntISConfiguration();
			}
			
			return instance;
		}
		
		public function config( scope:MovieClip, url:String )
		{
			if ( initFlag ) 
			{
				dispatchEvent( new Event( Event.COMPLETE ) );
				return;
			}
			
			initFlag = true;
			
			this.scope = scope;
			this.url = url;
			
			var loader:URLLoader = new URLLoader();
			configureListeners(loader);
			
			var request:URLRequest = new URLRequest( this.url );
			try {
				loader.load(request);
			} catch (error:Error) {
				trace("Unable to load requested document.");
			}
		}
		
		public function configureListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
        public function completeHandler(event:Event):void {
			
			trace( "@@@@@@@@@@@@@@@@@@@: start parse" );
            var loader:URLLoader = URLLoader(event.target);
			
			var lines: Array = unescape( loader.data ).split( "\r\n" );
			var line: String;
			var chunk: String;
			var value: String;
			var xmlurl: String;
			
			while( lines.length )
			{
				line = String( lines.shift() );
				
				//-- 주석 건너뜀
				if ( line.substring(0, 1 ) == "#" ||  line.substring(0, 1 ) == "" ) continue;
				
				//-- contents
				chunk = line.substr( 0, line.indexOf( "=" ) );
				value = line.substr( line.indexOf( "=" ) + 1, line.length );
				
				//trace( chunk + "=" + value )
				switch( chunk )
				{
					case "commandStart":
						if ( value == "start" ) exeFlag = true;
						break;
						
					case "commandEnd":
						if( value == "end" ) exeFlag = false;
						break;
						
					case "debuging":
						if ( value == "yes" ) debugFlag = true;
						break;
						
					case "output":
						if ( value == "yes" ) outputFlag = true 
						break;					
						
					case "path":
						path = value;
						break;	
						
					case "title":
						title = value;
						break;						
						
					case "eof":
						//종료
						initFlag = true;
						dispatchEvent( new Event( Event.COMPLETE ) );
						break;	
						
					default : 
						if ( exeFlag )
						{
							var classRefArr = chunk.split( "." );
							var classRef = "";
							
							if ( classRefArr.length == 2 )
							{
								trace( value, classRefArr[1].substr( 0, classRefArr[1].indexOf( "=" ) ) );
								scope[classRefArr[0]][ classRefArr[1].substr( 0, classRefArr[1].indexOf( "=" ) ) ] = value;
							}
							else
							{
								for ( var i = 0; i < classRefArr.length - 1; i++ ) 
								{
									classRef = classRef + classRefArr[i] + ".";
								}
								
								classRef = classRef.substring( 0,classRef.length - 1  );
								var property = classRefArr[i].substr( 0, classRefArr[i].indexOf( "=" ) );
								
								//trace( classRef, property, value );
								
								if ( value == "true" ) getDefinitionByName( classRef )[property] = true;
								if ( value == "false" ) getDefinitionByName( classRef )[property] = false;
								if ( value == "true" && value == "false" ) getDefinitionByName( classRef )[property] = value;
							}
						}
				}
			}
		}
		
		public function openHandler(event:Event):void {
			//trace("openHandler: " + event);
		}
		
		public function progressHandler(event:ProgressEvent):void {
			//trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
		}
		
		public function securityErrorHandler(event:SecurityErrorEvent):void {
			//trace("securityErrorHandler: " + event);
		}
		
		public function httpStatusHandler(event:HTTPStatusEvent):void {
			//trace("httpStatusHandler: " + event);
		}
		
		public function ioErrorHandler(event:IOErrorEvent):void {
			//trace("ioErrorHandler: " + event);
		}
	}
}

